package com.gemptc.daoimp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gemptc.dao.IRegrad;
import com.gemptc.utils.DBUtil;

public class RegradImp implements IRegrad {

	@Override
	public boolean Collect(int user_id, int focused_id, int c) {
		Connection connection = DBUtil.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		String sql = null;
		switch (c) {
		case 1:
			sql = "INSERT INTO regrad (user_id,focused_id) VALUES (?,?);";
			break;
		// 删除科普推荐
		case 2:
			sql = "DELETE FROM regrad WHERE user_id=? AND focused_id= ?;";
			break;
		case 3:
			sql = "SELECT r_id FROM regrad WHERE user_id=? AND focused_id=? ;";
			break;
		}

		boolean flag = false;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, user_id);
			preparedStatement.setInt(2, focused_id);
			if (c == 1 || c == 2) {
				preparedStatement.execute();
				flag = true;
			} else {
				resultSet = preparedStatement.executeQuery();
				while (resultSet.next()) {
					flag = true;
				}
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.release(resultSet, preparedStatement, connection);
		}
		return flag;
	}
}
